﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class CommonController : Controller
    {
        //
        // GET: /City/

        public ActionResult CitySelect()
        {
            return View();
        }
        public ActionResult PageSize()
        {
            return View();
        }
        public ActionResult imgs()
        {
            return View();
            //List<ADZ_GoodsIMG> list = new List<ADZ_GoodsIMG>();

            //ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //list = dbcontext.ADZ_GoodsIMG.Where(t => t.sort_NO == 1).OrderBy(t=>t.seq_id).Skip(1).Take(2).ToList();
            //return View(list);
        }
        public ActionResult imgsOne()
        {
            return View();
        }
        public ActionResult ImgsOneCommon()
        {
            return View();
        }
        public ActionResult getSelect(int selectType = 1)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            List<string> list = null;
            switch (selectType)
            {
                case 2:
                    var brandNames = from t in dbcontext.ADZ_BrandMaster
                                     where t.is_del == 0
                                     select t.brand_name;
                    list = brandNames.ToList();
                    break;
                case 3:
                    var categoryNames = from t in dbcontext.ADZ_GoodsCategory
                                        where t.is_del == 0
                                        select t.category_name;
                    list = categoryNames.ToList();
                    break;
                default:
                    var supplierNames = from t in dbcontext.ADZ_SupplierMaster
                                        where t.is_del == 0
                                        select t.supplier_name;
                    list = supplierNames.ToList();
                    break;
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult BankDropList(ElementAttribute EleAttr)
        {
            ViewBag.EleAttr = EleAttr;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var list = dbcontext.ADZ_Bank.ToList();
            return PartialView(list);
        }

        public ActionResult getExpress()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var expressNames = from t in dbcontext.ADZ_ExpressConfig
                               where t.is_used == 1
                               select t.express_name;
            List<string> list = expressNames.ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        public ActionResult selectmanageruser(ManagerQueryModel paramModel)
        {
            string loginName = paramModel.UserName ?? "";
            int userStatus = -10;
            string begTime = "";
            string endTime = "";
            int pageIndex = paramModel.pageIndex;
            int pageSize = paramModel.pageSize;

            ViewBag.paramModel = paramModel;

            var ds = UserBiz.Instance.GetPageList(loginName, userStatus, begTime, endTime, pageIndex, pageSize, paramModel.RoleID);
            var allrows = ConvertHelper.ConvertTo<int>(ds.GetTable(0).Rows[0]["allrows"], 0);
            IList<UserModel> userlist = new ModelHandler<UserModel>().FillModel(ds, 1);
            PagedList<UserModel> pagedlist = null;
            if (userlist != null && userlist.Count != 0)
                pagedlist = new PagedList<UserModel>(userlist, pageIndex, pageSize, allrows);
            return View(pagedlist);
        }

        public ActionResult getmanager()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            List<string> list = new List<string>();

            var ds = UserBiz.Instance.GetPageList("", -10, "", "", 1, 1000, -10);
            var table = ds.GetTable(1);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add($"{table.GetRow(i)["RealName"]}-{table.GetRow(i)["LoginName"]}-{table.GetRow(i)["uid"]}");
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult getbrands()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            List<string> list = new List<string>();

            var ls = dbcontext.ADZ_BrandMaster.Where(t => t.is_del == 0 && t.is_show == 1).OrderBy(t => t.brand_name).ToList();

            foreach (var item in ls)
            {
                list.Add($"{item.brand_name}");
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult getsuppliers()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            List<string> list = new List<string>();

            var ls = dbcontext.ADZ_SupplierMaster.Where(t => t.is_del == 0).OrderBy(t => t.supplier_name).ToList();

            foreach (var item in ls)
            {
                list.Add($"{item.supplier_name}");
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取供应商支付信息
        /// </summary>
        /// <param name="supplierdetails_id"></param>
        /// <returns></returns>
        public ActionResult GetpayInfo(long supplierdetails_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var aDZ_SupplierDetails = dbcontext.ADZ_SupplierDetails.FirstOrDefault(t => t.seq_id == supplierdetails_id);
            return Json(aDZ_SupplierDetails, JsonRequestBehavior.AllowGet);
        }

        public ActionResult jiesuanlog()
        {
            return View();
        }



        #region

        [NoVerify]
        public string GetXQBOrderGoodsList(string order_no)
        {
            string sql = @"SELECT a.order_id,

                           a.order_no,
					       b.goods_id,
						   a.create_time,
					       c.supplier_id,
					       c.mpn,
					       c.dcn,
						   c.currency_type,
						   CASE c.currency_type WHEN 0 THEN '人民币' WHEN 1 THEN '美金' END AS currency,
					       d.category_name,
					       e.brand_name,
					       c.goods_unit,
					       c.supply_price * 1000 AS goods_price,
                           b.sell_price * 1000 AS sale_price,
                           c.supply_price* b.amount AS goods_total_amount,
						   b.total_money - c.supply_price * b.amount AS commission_amount,
                           b.amount AS sale_num,
					       1 - (c.supply_price / b.sell_price) AS rate,
                            b.total_money,
					       f.supplier_name
                      FROM ADZ_OrderMaster AS a
                      LEFT JOIN ADZ_OrderDetails AS b ON a.order_id = b.order_id

                      LEFT JOIN ADZ_GoodsMaster AS c ON c.goods_id = b.goods_id

                      LEFT JOIN ADZ_GoodsCategory AS d ON d.category_id = c.category_id

                      LEFT JOIN ADZ_BrandMaster AS e ON e.brand_id = c.brand_id

                      LEFT JOIN ADZ_SupplierMaster AS f ON f.supplier_id = c.supplier_id

                     WHERE a.order_no = '"+ order_no + "'";
            DataSet ds = YiYiTong.Rocket.DataAccess.GoodsDataAccess.Instance.GetXQBOrderGoodsList(sql);
            return JsonHelper.Serialize(ds.GetTable(0));
        }
        #endregion
    }
}